package com.yikaiye.android.yikaiye.ui.activity.home;

import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

import com.umeng.socialize.bean.SHARE_MEDIA;
import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.base.BaseActivity;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.databinding.ActivityDetailsBinding;
import com.yikaiye.android.yikaiye.dialog.ShareDialog;
import com.yikaiye.android.yikaiye.ui.activity.home.viewmodel.ProductDetailsViewModel;
import com.yikaiye.android.yikaiye.ui.adapter.ViewPagerAdapter;
import com.yikaiye.android.yikaiye.ui.fragment.DetailsFragment;
import com.yikaiye.android.yikaiye.ui.fragment.ProductFragment;
import com.yikaiye.android.yikaiye.utils.ScreenUtil;
import com.yikaiye.android.yikaiye.utils.UMengUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 产品详情页
 */
public class ProductDetailsActivity extends BaseActivity<ActivityDetailsBinding, ProductDetailsViewModel> {

    private final List<String> titles = new ArrayList<>();
    private final ArrayList<Fragment> fragments = new ArrayList<>();
    private String id;
    private ShareDialog shareDialog;

    @Override
    protected ProductDetailsViewModel bindModel() {
        return getViewModel(this, ProductDetailsViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void initData() {
        UMengUtil.setCountEventId(Constant.PAGE_PRODUCT_DETAIL);
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) binding.rlTitle.getLayoutParams();
        params.topMargin = ScreenUtil.getStatusBarHeight(context);
        binding.rlTitle.setLayoutParams(params);

        if (null != getIntent().getExtras()) {
            id = getIntent().getExtras().getString(Constant.ID_KEY);
        }
        titles.add(getString(R.string.product_text));
        titles.add(getString(R.string.details_text));
        showLoadingDialog();
        viewModel.getProductDetails(id);
        initDialog();
    }


    @Override
    protected void initClick() {
        viewModel.onDelayClick(binding.ivBack, o -> finish());
        viewModel.onDelayClick(binding.ivShare, o -> {
            if (null != shareDialog) {
                shareDialog.showDialog();
            }
            UMengUtil.setCountEventId(Constant.PAGE_DETAILS_SHARE);
        });

        viewModel.productDetailDataModelMutableLiveData.observe(this, productDetailDataModel -> {
            if (null != productDetailDataModel) {
                fragments.add(ProductFragment.newInstance(productDetailDataModel));
                fragments.add(DetailsFragment.newInstance(productDetailDataModel.getProductDetail()));
                ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), 0, titles, fragments);
                binding.viewPager.setAdapter(adapter);
                binding.tabLayout.setViewPager(binding.viewPager);
            }
            dismissLoadingDialog();
        });
        viewModel.productDetailErrorMutableLiveData.observe(this, s -> dismissLoadingDialog());
    }

    private void initDialog() {
        shareDialog = new ShareDialog(this) {

            @Override
            public void share(SHARE_MEDIA shareMedia) {
//                ToastHelper.show("分享内容待定");
//                ShareUtil.shareWeb(ProductDetailsActivity.this,
//                        "https://developer.umeng.com/docs/128606/detail/193883",
//                        "百度", "测试", null, shareMedia);
            }

            @Override
            public void cancel() {

            }
        };
    }
}